package com.example.beikeyigou.history;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.goods.GoodsBean;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.pay.PayAdapter;
import com.example.beikeyigou.trolley.TrolleyBean;
import com.example.beikeyigou.utils.BaseActivity;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends BaseActivity {
    ListView historyLv;
    ImageView backImg;

    UserBean.DataBean userData;
    List<TrolleyBean.DataBean> data;
    PayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        
        findView();
        initData();
        setListener();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        userData = SaveUserUtils.getUserData(this);
        if (userData == null) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        data = new ArrayList<>();
        adapter = new PayAdapter(this, data);
        historyLv.setAdapter(adapter);
        getNetwork(URLUtils.querybougth_url + "?userId=" + String.valueOf(userData.getId()));
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TrolleyBean goodsBean = new Gson().fromJson(result, TrolleyBean.class);
        List<TrolleyBean.DataBean> list = goodsBean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();
    }

    private void findView() {
        backImg = findViewById(R.id.history_iv_back);
        historyLv = findViewById(R.id.history_lv);
    }
}